package org.swift.mashup.engine.deployment.mcr;

import org.apache.axis2.context.MessageContext;
import org.apache.axis2.transport.http.HTTPConstants;
import org.swift.mashup.engine.deployment.MessageContextReader;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

public class CookieMessageContextReader implements MessageContextReader {

	private static CookieMessageContextReader instance = null;

	public static CookieMessageContextReader getInstance() {
		if (instance == null) {
			instance = new CookieMessageContextReader();
		}
		return instance;
	}

	private CookieMessageContextReader() {}

	@Override
	public Map<String, Object> createParameterMap(MessageContext context) {
		Map<String,Object> paramMap = new HashMap<String,Object>();
		HttpServletRequest req = (HttpServletRequest)context.getProperty(HTTPConstants.MC_HTTP_SERVLETREQUEST);

        Cookie[] cookies = req.getCookies();
        if ( cookies != null ) {
            for ( Cookie cookie : cookies ) {
                paramMap.put( cookie.getName(), cookie.getValue() );
            }
        }

		return paramMap;
	}
}
